package com.xinrui.sunnyday.controller;

import com.xinrui.sunnyday.constant.MessageConstant;
import com.xinrui.sunnyday.constant.RedisMessageConstant;
import com.xinrui.sunnyday.entity.Result;
import com.xinrui.sunnyday.exception.SunnydayException;
import com.xinrui.sunnyday.pojo.Order;
import com.xinrui.sunnyday.utils.JedisUtils;
import com.xinrui.sunnyday.utils.SMSUtils;
import com.xinrui.sunnyday.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@RestController
@RequestMapping("/validateCode")
public class ValidateCodeController {

    @Autowired
    private JedisPool jedisPool;

    @PostMapping("/send4Order")
    public Result send4Order(String telephone) {
        //获取jedis连接池中的连接
//        Jedis jedis = jedisPool.getResource();
        //拼接一个key的名称
        String key = RedisMessageConstant.SENDTYPE_ORDER + "_" + telephone;
        //存入redis
        String codeInRedis = jedisPool.getResource().get(key);
        //redis里面不存在验证码 发送验证码
        if (null == codeInRedis) {
            //获取生成的随机验证码
            Integer validateCode = ValidateCodeUtils.generateValidateCode(4);
            System.out.println("code = " + validateCode);
            //腾讯云需要String[]来替代短信模板的参数
            String[] params = new String[]{validateCode+""};
            try {
                //2.2调用SMSUtils发送验证码
                SMSUtils.sendSingleMessageForCode(telephone, params);
                //2.3把验证码存入redis(5分钟有效期)，value:验证码, key:手机号码
                jedisPool.getResource().setex(key, 5*60, validateCode + "");
                // 返回成功
                return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
            } catch (Exception e) {
                e.printStackTrace();
                // 发送失败
                return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
            }
        }
        return new Result(false, MessageConstant.SENT_VALIDATECODE);
    }

    @PostMapping("/send4Login")
    public Result send4Login(String telephone){
        //获取redis连接池的连接
        Jedis jedis = jedisPool.getResource();
        //拼接redis的key
        String key = RedisMessageConstant.SENDTYPE_LOGIN + "_" + telephone;
        //将key存入redis中
        String codeInRedis = jedis.get(key);
        //判断redis里有无验证码
        if (null == codeInRedis){
            //没有验证码 可以发送
//获取生成的随机验证码
            Integer validateCode = ValidateCodeUtils.generateValidateCode(4);
            System.out.println("code = " + validateCode);
            //腾讯云需要String[]来替代短信模板的参数
            String[] params = new String[]{validateCode+""};
            try {
                //2.2调用SMSUtils发送验证码
                SMSUtils.sendSingleMessageForCode(telephone, params);
                //2.3把验证码存入redis(5分钟有效期)，value:验证码, key:手机号码
                jedisPool.getResource().setex(key, 5*60, validateCode + "");
                // 返回成功
                return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
            } catch (Exception e) {
                e.printStackTrace();
                // 发送失败
                return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
            }
        }
        return new Result(false,MessageConstant.SENT_VALIDATECODE);
    }
}
